package com.xuxin.baby.model;

import com.blankj.utilcode.util.LogUtils;
import com.xuxin.baby.bean.AuthenticateBean;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.LoginBean;
import com.xuxin.baby.bean.TenantIdBean;
import com.xuxin.baby.contract.LoginContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.LoginPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * Created by xu_xin
 * Created on 2020/10/22 0022
 * 验证码登录
 */
public class LoginModel implements LoginContract.Model {
    @Override
    public void sendCode(LoginPresenter presenter, String phone, String code) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .sendCode("1", phone, code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<BaseBean>() {
                    @Override
                    public void onNextX(@NonNull BaseBean baseBean) {
                        if (baseBean.isSuccess()) {
                            presenter.onSuccess(baseBean);
                        } else {
                            presenter.onError(baseBean);
                        }
                    }

                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void getTenantId(LoginPresenter presenter, String tenancyName) {
        try {
            JSONObject object = new JSONObject();
            object.put("tenancyName", tenancyName);
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .getTenantId("1", requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new myObserver<TenantIdBean>() {
                        @Override
                        public void onNextX(@NonNull TenantIdBean baseBean) {
                            if (baseBean.isSuccess()) {
                                presenter.onSuccess(baseBean);
                            } else {
                                presenter.onError(baseBean);
                            }
                        }

                        @Override
                        public void onError(BaseBean baseBean) {
                            presenter.onError(baseBean);
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void authenticate(LoginPresenter presenter, String tenantId, String userNameOrEmailAddress, String password) {
        try {
            JSONObject object = new JSONObject();
            object.put("userNameOrEmailAddress", userNameOrEmailAddress);
            object.put("password", password);
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .authenticate(tenantId, "1", requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new myObserver<AuthenticateBean>() {
                        @Override
                        public void onNextX(@NonNull AuthenticateBean baseBean) {
                            if (baseBean.isSuccess()) {
                                presenter.onSuccess(baseBean);
                            } else {
                                presenter.onError(baseBean);
                            }
                        }

                        @Override
                        public void onError(BaseBean baseBean) {
                            presenter.onError(baseBean);
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void login(LoginPresenter presenter, String tenantId, String token, String phone) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .login(tenantId, token, "1", phone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<LoginBean>() {
                    @Override
                    public void onNextX(@NonNull LoginBean baseBean) {
                        if (baseBean.isSuccess()) {
                            presenter.onSuccess(baseBean);
                        } else {
                            presenter.onError(baseBean);
                        }
                    }

                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void getInfo(LoginPresenter presenter, String tenantId, String token) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getInfo(tenantId, token, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<Response<ResponseBody>>() {
                    @Override
                    public void onNext(@NonNull Response<ResponseBody> baseBean) {
                        try {
                            LogUtils.e("ResponseBody" + baseBean.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }
}